package handler

import (
	"fmt"
	"choujiang/box_server/service"
	"choujiang/common/utils"

	"github.com/gin-gonic/gin"
	"choujiang/common/consts"
	"choujiang/common/db"
	"choujiang/common/models"
	"strconv"
)

func GameList(c *gin.Context) {
	plat := c.Request.Form.Get("plat")
	gameId := c.Request.Form.Get("gameId")
	fmt.Printf("plat:%+v\n", plat)
	uniqueId := c.Request.Form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	if gameId == "" {
		gameId = "leyoubox"
	}
	p := utils.PLAT[plat]

	ret := make(map[string]interface{})
	hot := service.GetGameList(p, 1, gameId, user.Id)
	ret["hot"] = hot
	news := service.GetGameList(p, 2, gameId, user.Id)
	ret["news"] = news

	banner := service.GetBannerList(0)
	ret["banner"] = banner

	recommend := service.GetRecommendList(p, 5, gameId, user.Id, true)
	ret["recommend"] = recommend

	played_recommend := service.GetPlayedRecommend(user, gameId)
	ret["played_recommend"] = played_recommend

	to_receive_recommend := service.GetToReceiveRecommend(user, gameId)
	ret["to_receive"] = to_receive_recommend

	category_game := service.GetCategoryGames()
	ret["category"] = category_game

	utils.JSON(ret, c)
}

func MoreRecommend(c *gin.Context) {
	plat := c.Request.Form.Get("plat")
	uniqueId := c.Request.Form.Get("uniqueId")
	gameId := c.Request.Form.Get("gameId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	if gameId == "" {
		gameId = "leyoubox"
	}
	p := utils.PLAT[plat]
	ret := make(map[string]interface{})
	recommend := service.GetRecommendList(p, 0, gameId, user.Id, true)
	ret["recommend"] = recommend
	ret["recommend_num"] = service.GetRecommendNum(p)
	played_recommend := service.GetPlayedRecommend(user, gameId)
	ret["played_recommend"] = played_recommend

	to_receive_recommend := service.GetToReceiveRecommend(user, gameId)
	ret["to_receive"] = to_receive_recommend

	utils.JSON(ret, c)
}

func ChangeBatch(c *gin.Context) {
	pageNo := c.Request.Form.Get("pageNo")
	pageSize := c.Request.Form.Get("pageSize")
	uniqueId := c.Request.Form.Get("uniqueId")
	aType := c.Request.Form.Get("type")
	category := c.Request.Form.Get("category")
	gameId := c.Request.Form.Get("gameId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	if gameId == "" {
		gameId = "leyoubox"
	}
	int64No, _ := strconv.Atoi(pageNo)
	int64Size, _ := strconv.Atoi(pageSize)
	int64Type, _ := strconv.Atoi(aType)
	int64Category, _ := strconv.Atoi(category)
	ret := make(map[string]interface{})
	if int64Type == 99 {
		played_recommend := service.GetPlayedRecommend(user, gameId)
		ret["played_recommend"] = played_recommend
		ret["reward_point"] = 50
		to_receive_recommend := service.GetToReceiveRecommend(user, gameId)
		ret["to_receive"] = to_receive_recommend
	} else if int64Type == 3 {
		ret["games"] = service.GetMoreCategory(int64Category)
	} else {
		games, hasMore := service.GetGamesByPage(int64No, int64Size, int64Type)
		ret["games"] = games
		ret["hasMore"] = hasMore
	}
	utils.JSON(ret, c)
}

func NewGameList(c *gin.Context) {
	plat := c.Request.Form.Get("plat")
	gameId := c.Request.Form.Get("gameId")
	uniqueId := c.Request.Form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	p := utils.PLAT[plat]
	ret := make(map[string]interface{})
	recommend := service.GetRecommendList(p, 0, gameId, user.Id, true)
	ret["recommend"] = recommend

	played_recommend := service.GetPlayedRecommend(user, gameId)
	ret["played_recommend"] = played_recommend

	to_receive_recommend := service.GetToReceiveRecommend(user, gameId)
	ret["to_receive"] = to_receive_recommend

	task := service.GetNewTaskInfo(user)
	ret["task"] = task

	members := service.GetMembers(user.Id, gameId)
	ret["members"] = members
	ret["game_nums"] = 8
	ret["invite_nums"] = 3

	utils.JSON(ret, c)
}

/**
 @description 获取首页通知接口
 */
func Messages(c *gin.Context) {
	gameId := c.Request.Form.Get("gameId")
	messages := service.GetMessages(gameId)

	utils.JSON(messages, c)
}

/**
 @description 获取首页信息接口
 @return mix
 @author guxiang
 @version v1 2018-11-23
 */
func HomeIndex(c *gin.Context) {
	plat := c.Request.Form.Get("plat")
	uniqueId := c.Request.Form.Get("uniqueId")
	token := c.Request.Form.Get("shareToken")
	fromGame := c.Request.Form.Get("fromGame")
	gameId := c.Request.Form.Get("gameId")
	scene := c.Request.Form.Get("scene")
	p := utils.PLAT[plat]
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	ret := make(map[string]interface{})
	//轮播图信息
	banner := service.GetBannerList(0)
	ret["banner"] = banner
	reInt, shareStruct := service.ValidateShareToken(token)
	fmt.Println("------------", shareStruct)
	if reInt != 0 { //shareToken 有效
		//好友送红包游戏信息
		games := service.GetFriendGames(p, shareStruct.UserId)
		ret["games"] = games
		//邀请人信息
		ret["inviter"] = model.GetUserByUid(shareStruct.UserId)
		//邀请游戏信息
		if fromGame != "" {
			game := model.GetGameByGameId(fromGame)
			game.Icon = utils.AddPrefix(game.Icon)
			game.InviterEarn = utils.Decimal(game.Price * 0.3)
			game.PlayerEarn = utils.Decimal(game.Price * 0.1)
			ret["game"] = game
		}
	} else {
		if scene != "" { //scene不为空，用户通过二维码进入
			fmt.Printf("scene 值不为空 %s \n", scene)
			inviterId := db.Mgo.GetUserIdFromScene(scene)
			fmt.Printf("inviterId 的值为 %s \n", inviterId)
			if inviterId != 0 { //scene有效
				//好友送红包游戏信息
				games := service.GetFriendGames(p, inviterId)
				ret["games"] = games
				//邀请人信息
				ret["inviter"] = model.GetUserByUid(inviterId)
				//邀请游戏信息
				if fromGame != "" {
					game := model.GetGameByGameId(fromGame)
					game.Icon = utils.AddPrefix(game.Icon)
					game.InviterEarn = utils.Decimal(game.Price * 0.3)
					game.PlayerEarn = utils.Decimal(game.Price * 0.1)
					ret["game"] = game
				}
			}
		}
	}
	//获取兑换物品信息
	//goods, _ := service.GetGoodsList()
	//ret["goods"] = goods
	//获取首页广告信息
	advert := service.GetAdvertList(1)
	ret["advert"] = advert
	information := model.GetAdvertList(2)
	ret["information"] = information
	//用户信息
	ret["user"] = model.GetUserExtraByUid(user.Id)
	//头条信息
	top := service.GetTopLine()
	ret["top"] = top
	//0元促销游戏
	special := service.GetSpecialGames(gameId, user.Id)
	ret["special"] = special
	//已付费游戏
	promote := service.GetPromoteGame(gameId, user.Id, 1)
	ret["promote"] = promote
	//未付费游戏
	unPromote := service.GetPromoteGame(gameId, user.Id, 0)
	ret["unpromote"] = unPromote

	utils.JSON(ret, c)
}

/**
 @description 获取兑奖首页信息接口
 */
func HomePage(c *gin.Context) {
	uniqueId := c.Request.Form.Get("uniqueId")
	gameId := c.Request.Form.Get("gameId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	ret := make(map[string]interface{})
	//获取抽奖商品
	goods, _ := service.GetGoodsList()
	ret["goods"] = goods
	//我的好友
	friends, total := service.GetMyFriends(user.Id)
	ret["friends"] = friends
	ret["total"] = total
	//体验游戏
	games := service.GetGameList(0, 1, gameId, user.Id)
	ret["games"] = games
	//每日奖励
	ret["daily_bonus"] = service.GetDailyBonus(user)

	utils.JSON(ret, c)
}
